<?php

namespace app\base\controller\miniapp\staff;

use app\base\model\member\MemberModel;

use app\BaseController;



class Main extends BaseController{

    public function getStaffInfo()

    {

        $info=[

            'face'=>$this->request->member->face,

            'name'=>$this->request->staff->name,

            'mobile'=>$this->request->staff->mobile,

            'apply_vip'=>in_array(1,$this->request->staff->add_value_jurisdiction) ? 1 : 0,

            'write_off_jurisdiction'=>$this->request->staff->write_off_jurisdiction,

            'add_value_jurisdiction'=>$this->request->staff->add_value_jurisdiction,

        ];

        return $this->success(['info'=>$info]);

    }



    public function getSearchMemeberId()

    {

        $mobile = $this->request->param('mobile');

        if(empty($mobile) || !is_mobile($mobile)){

            return $this->error('请输入手机号');

        }



        if (null === $member = MemberModel::where([

                ['mobile','=',$mobile],

                ['shop_id','=',$this->request->shopId],

                ['status','=',1],

                ['is_delete','=',0]

            ])->find()) {

            return $this->error('会员不存在');

        }

        return $this->success(['member_id'=>$member->member_id]);

    }



    public function scanCheckMember()

    {

        $member_id = $this->request->param('member_id/d',0);

        if(null === $member = MemberModel::find($member_id)) return $this->error('用户不存在');

        if($member->shop_id != $this->request->shopId) return $this->error('用户不存在');

        if($member->status != 1) return $this->error('用户不存在');

        if($member->is_delete !=0) return $this->error('用户不存在');



        $code = $this->request->param('code');

        if(empty($code)) return $this->error('会员码不正确');

        if($member->code != $code) return $this->error('会员码不正确');



        return $this->success([]);

    }



    public function applyVip()

    {

        //判断权限

        if (!in_array(1, $this->request->staff->add_value_jurisdiction)) {

            return $this->error('您没有办理会员的权限');

        }



        $nick_name=$this->request->param('nick_name');

        $mobile=$this->request->param('mobile');

        $birthday=$this->request->param('birthday');



        if(empty($nick_name) || mb_strlen($nick_name) >32) return $this->error('请输入昵称且昵称长度不能多余32');

        if(empty($mobile) || !is_mobile($mobile)) return $this->error('请输入手机号');

        if(empty($birthday)) return $this->error('请选择生日');

        if(false === strtotime($birthday." 00:00:00")) return $this->error('请选择正确的生日');

        $birthday_arr=explode('-',$birthday);

        if(count($birthday_arr) !=3) return $this->error('请选择正确的生日');

        $birthday_year=$birthday_arr[0]; $birthday_month=$birthday_arr[1]; $birthday_day=$birthday_arr[2];



        //判断手机号

        if(null !== MemberModel::where([

                ['shop_id','=',$this->request->shopId],

                ['mobile','=',$mobile],

                ['status','=',1],

                ['is_delete','=',0]

            ])->find()){

            return $this->error('该手机号已经存在了');

        }



        $member = new MemberModel();

        $member->shop_id =$this->request->shopId;

        $member->nick_name=$nick_name;

        $member->mobile=$mobile;

        $member->birthday_year=$birthday_year;

        $member->birthday_month=$birthday_month;

        $member->birthday_day=$birthday_day;

        $member->save();



        return $this->success(['member_id'=>$member->member_id]);

    }





}